import axios from 'axios';
/* import { Message } from 'element-ui'; */
import Vue from 'vue';
let localhosts = 'http://www.xxx.com'; //请求的后台域名
axios.interceptors.request.use(config => { //请求之前(可以设置token)
  return config
}, error => {
  this.$notification.error({
    message: '错误提示',
    description: error,
    icon: <a-icon type = "smile"
    style = "color: #108ee9" / > ,
  });
  //Message.error(error)
  return Promise.reject(error);
});
axios.interceptors.response.use(response => { //数据拿到之后
  return response.data
}, error => {
  this.$notification.error({
    message: '错误提示',
    description: 'Http请求失败，请联系管理员',
    icon: <a-icon type = "smile"
    style = "color: #108ee9" / > ,
  });
  //Message.error('Http请求失败，请联系管理员');
  return Promise.reject(error.response);;
});

function successfun(res) { //处理后台返回的非200错误
  if (res.code === 200) {
    return res
  } else {
    this.$notification.error({
      message: '错误提示',
      description: res.message,
      icon: <a-icon type = "smile"
      style = "color: #108ee9" / > ,
    });
    //Message.warning(res.message);
    return res;
  }
}

function errorfun(res) {
  if (res.code != 200) {
    this.$notification.error({
      message: '错误提示',
      description: res.message,
      icon: <a-icon type = "smile"
      style = "color: #108ee9" / > ,
    });
    //Message.error(res.message);
    return res;
  }
}
export default {
  post(url, data) { //post请求
    return axios({
      method: 'post',
      baseURL: localhosts,
      url,
      data: data,
      withCredentials: true,
      timeout: 5000, //响应时间
    }).then(res => {
      return successfun(res)
    }, err => {
      return errorfun(err);
    })
  },
  get(url, params) { //get请求
    return axios({
      method: 'get',
      baseURL: localhosts,
      url,
      params,
      withCredentials: true,
      timeout: 5000,
    }).then(res => {
      return successfun(res)
    }, err => {
      return errorfun(err)
    })
  }
  //(delete,put等请求同上)...
}
